<?php
class Upload extends MY_Controller {
 
	function __construct() {
		parent::__construct();
		$this->load->model('photo_model', '', true);
		$this->load->model('user_model', '', true);
		$this->load->model('society_model', '', true);
		$this->load->helper('form');
	}
	
	function upload_init($category) {
		$user_id = $_SESSION['id'];
		$user_confirm_flag = $_SESSION['confirm_FLAG'];
		
		$data['photo_c_name'] = $this->common_data->_get_society_photo_category_name_by_cid($category);
		$data['error'] = '';
		$data['category'] = $category;
		
		$condition['society_id'] = $user_id;
		$condition['category'] = $category;
		$condition['sort_upload_time'] = DESC;
		$data['img_list'] = $this->photo_model->search_by_condition($condition);
		
		$data['remaining_count'] = $this->get_remaining_upload_count($category);
		
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
		$this->load->view('backend/society/upload_pic', $data);
	}
	
	
	function do_upload() {
		$data['current_menu_tab'] = MENU_TAB_A;
		$user_id = $_SESSION['id'];
		
		$user_temp = $this->user_model->get_by_id($user_id);
		$user_create_time = date("Y/m/d", $user_temp['create_time']);
		$category = $this->input->post('category');
		
		$condition['society_id'] = $user_id;
		$data['society'] = $this->society_model->search_by_condition($condition, TRUE);
		
		$data['remaining_count'] = $this->get_remaining_upload_count($category);
		//判断该分类还能否上传图片，若剩余量小于1（不能上传），则返回
		if ($data['remaining_count'] < 1) {
			redirect(BACKEND_SOCIETY_UPLOAD_PATH . $category);
			return;
		}
		
		/**
		 *按照建户时间及用户ID创建的图片上传专用文件夹 
		 */
		$folder_path = $_SERVER['DOCUMENT_ROOT'] . '/' . SYSTEM_FOLDER . '/' . USER_UPLOAD_PIC_FOLDER . $user_create_time . '/' . $user_id;
		$this->common->createDirByCurrentDate($folder_path);
		
		//文件上传的设置
		$config['upload_path'] = './' . USER_UPLOAD_PIC_FOLDER . $user_create_time . '/' . $user_id;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '1000';
		$config['max_width']  = '10240';
		$config['max_height']  = '7680';
		$config['encrypt_name'] = 'TRUE';
		$this->load->library('upload', $config);
		
		if (!$this->upload->do_upload()) {
			$data['photo_c_name'] = $this->common_data->_get_society_photo_category_name_by_cid($category);
			$data['error'] = $this->upload->display_errors();
			$data['category'] = $category;
			
			$condition['society_id'] = $user_id;
			$condition['category'] = $category;
			$condition['sort_upload_time'] = DESC;
			$data['img_list'] = $this->photo_model->search_by_condition($condition);
			
			////////////////已登陆用户信息绑定/////////////
			$data['user_info'] = $this->login_user_info();
			
			////////////////用户后台侧栏信息绑定/////////////
			$data['sidebar_info'] = $this->sidebar_info();
			
			$this->load->view('backend/society/upload_pic', $data);
		} else {
			$upload_data = $this->upload->data();
			//传入数据库的图片地址
			$photo_path_db = USER_UPLOAD_PIC_FOLDER . $user_create_time . '/' . $user_id . '/' . $upload_data['file_name'];
			$thumb_path_db = USER_UPLOAD_PIC_FOLDER . $user_create_time . '/' . $user_id . '/' . $upload_data['raw_name'] . '_thumb' . $upload_data['file_ext'];
			$db_data['society_id'] = $_SESSION['id'];
			$db_data['photo_path'] = $photo_path_db;
			$db_data['upload_time'] = time();
			$db_data['category'] = $category;
			$db_data['thumb_path'] = $thumb_path_db;
			$this->make_thumb($upload_data['full_path'], $category);
			$this->photo_model->save($db_data);
			
			if ($category == SOCIETY_LOGO) {
				$this->session->set_userdata(SESSION_SOCIETY_LOGO_THUMB, $thumb_path_db);
			}
			
			redirect(BACKEND_SOCIETY_UPLOAD_PATH . $category);		
		}
	}
	
	/**
	 * 创建缩略图
	 * @param $source_path
	 */
	function make_thumb($source_path, $category) {
		$config['image_library'] = 'gd2';
		$config['source_image'] = $source_path;
		$config['create_thumb'] = TRUE;
		$config['maintain_ratio'] = TRUE;
		
		switch ($category) {
			case SOCIETY_LOGO:
				$config['width'] = 128;
				$config['height'] = 128;
				break;
			case SOCIETY_BANNER:
				$config['width'] = 300;
				$config['height'] = 200;
				break;
			case SOCIETY_SHOW:
				$config['width'] = 400;
				$config['height'] = 300;
				break;
		}
		
		$this->load->library('image_lib', $config); 
		$this->image_lib->resize();
	}
    
    function delete($id) {
    	$data = $this->photo_model->get_by_id($id);
    	$data['photo_path'] = $_SERVER["DOCUMENT_ROOT"] . '/' . SYSTEM_FOLDER . '/' . $data['photo_path'];
    	$data['thumb_path'] = $_SERVER["DOCUMENT_ROOT"] . '/' . SYSTEM_FOLDER . '/' . $data['thumb_path'];
    	//删除图片文件及其缩略图
    	if(file_exists($data['photo_path']) OR file_exists($data['thumb_path'])) {
			unlink($data['photo_path']);
			unlink($data['thumb_path']);
		}
		//删除数据库中的文件
    	$this->photo_model->delete($id);
    	
    	if ($data['category'] == SOCIETY_LOGO) {
			$this->session->set_userdata(SESSION_SOCIETY_LOGO_THUMB, "");
		}
			
    	redirect(BACKEND_SOCIETY_UPLOAD_PATH . $data['category']);
    }
    
    // 1:logo  2:banner 3:社团风采展
    function get_remaining_upload_count($category) {
    	$condition['society_id'] = $_SESSION['id'];
		$condition['category'] = $category;
		$count = $this->photo_model->search_by_condition($condition, "", TRUE);
		if ($category == SOCIETY_LOGO) {
			$remaining_count = SOCIETY_LOGO_COUNT - $count;
		} elseif ($category == SOCIETY_BANNER) {
			$remaining_count = SOCIETY_BANNER_COUNT - $count;
		} elseif ($category == SOCIETY_SHOW) {
			$remaining_count = SOCIETY_SHOW_COUNT - $count;
		}
    	return $remaining_count;
    }
    
}
?>